using MediatR;
using Microsoft.AspNetCore.Mvc;
using S.M.SmartMedical.API.Read.Application.Query;
using S.M.SmartMedical.Domain.DTOs;
using S.M.SmartMedical.ErrorCode;

namespace S.M.SmartMedical.API.Read.Controllers
{
    /// <summary>
    /// 医院管理控制器
    /// 提供医院信息的查询接口
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class HospitalController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<HospitalController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        public HospitalController(IMediator mediator, ILogger<HospitalController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 获取医院列表（分页）
        /// </summary>
        /// <param name="name">医院名称关键字</param>
        /// <param name="level">医院等级</param>
        /// <param name="type">医院类型</param>
        /// <param name="currentPage">当前页码，默认1</param>
        /// <param name="pageSize">每页记录数，默认10</param>
        /// <returns>医院分页列表</returns>
        [HttpGet]
        public async Task<ActionResult<APIResult<PageResponse<HospitalDto>>>> GetHospitals(
            [FromQuery] string? name = null,
            [FromQuery] string? level = null,
            [FromQuery] string? type = null,
            [FromQuery] int currentPage = 1,
            [FromQuery] int pageSize = 10)
        {
            try
            {
                _logger.LogInformation("接收到获取医院列表请求，参数：name={name}, level={level}, type={type}, currentPage={currentPage}, pageSize={pageSize}",
                    name, level, type, currentPage, pageSize);

                var query = new GetHospitalsQuery
                {
                    Name = name,
                    Level = level,
                    Type = type,
                    CurrentPage = currentPage,
                    PageSize = pageSize
                };

                var result = await _mediator.Send(query);
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取医院列表时发生错误");
                return StatusCode(500, new APIResult<PageResponse<HospitalDto>>
                {
                    Code = APIEnum.Fail,
                    Message = "服务器内部错误"
                });
            }
        }

        /// <summary>
        /// 根据ID获取医院详情
        /// </summary>
        /// <param name="id">医院ID</param>
        /// <returns>医院详细信息</returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<APIResult<HospitalDto>>> GetHospitalById(int id)
        {
            try
            {
                _logger.LogInformation("接收到获取医院详情请求，ID：{Id}", id);

                var query = new GetHospitalByIdQuery(id);
                var result = await _mediator.Send(query);

                if (result.Code == APIEnum.OK)
                {
                    return Ok(result);
                }
                else
                {
                    return NotFound(result);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取医院详情时发生错误，ID：{Id}", id);
                return StatusCode(500, new APIResult<HospitalDto>
                {
                    Code = APIEnum.Fail,
                    Message = "服务器内部错误"
                });
            }
        }
    }
}